<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false">
		<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="100px" @keyup.enter="submitHandle()">
			<el-form-item label="主键" prop="id" v-if="false">
				<el-input v-model="dataForm.id" placeholder="主键"></el-input>
			</el-form-item>

			<el-form-item label="关联用车申请单" prop="isApproval">
				<el-input v-model="dataForm.isApproval" placeholder="关联用车申请单"></el-input>
			</el-form-item>
			<el-form-item label="用车申请单id" prop="approvalId">
				<el-input v-model="dataForm.approvalId" placeholder="用车申请单id"></el-input>
			</el-form-item>
			<el-form-item label="车辆id" prop="carId">
				<el-input v-model="dataForm.carId" placeholder="车辆id"></el-input>
			</el-form-item>
			<el-form-item label="申请人id" prop="staffId">
				<el-input v-model="dataForm.staffId" placeholder="申请人id"></el-input>
			</el-form-item>
			<el-form-item label="出车时间" prop="startDate">
				<el-input v-model="dataForm.startDate" placeholder="出车时间"></el-input>
			</el-form-item>
			<el-form-item label="出车地点" prop="startSite">
				<el-input v-model="dataForm.startSite" placeholder="出车地点"></el-input>
			</el-form-item>
			<el-form-item label="回车时间" prop="endDate">
				<el-input v-model="dataForm.endDate" placeholder="回车时间"></el-input>
			</el-form-item>
			<el-form-item label="回车地点" prop="endSite">
				<el-input v-model="dataForm.endSite" placeholder="回车地点"></el-input>
			</el-form-item>
			<el-form-item label="仪表盘里程" prop="mileage">
				<el-input v-model="dataForm.mileage" placeholder="仪表盘里程"></el-input>
			</el-form-item>
			<el-form-item label="用车事由" prop="reasons">
				<el-input v-model="dataForm.reasons" placeholder="用车事由"></el-input>
			</el-form-item>
			<el-form-item label="车况备注" prop="remark">
				<el-input v-model="dataForm.remark" placeholder="车况备注"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="submitHandle()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import { useReturn_carsApi, useReturn_carsSubmitApi } from '@/api/nb/car/return_cars'

const emit = defineEmits(['refreshDataList'])

const visible = ref(false)
const dataFormRef = ref()

const dataForm = reactive({
	id: '',

	isApproval: '',
	approvalId: '',
	carId: '',
	staffId: '',
	startDate: '',
	startSite: '',
	endDate: '',
	endSite: '',
	mileage: '',
	reasons: '',
	remark: ''
})

const init = (id?: number) => {
	visible.value = true
	dataForm.id = ''

	// 重置表单数据
	if (dataFormRef.value) {
		dataFormRef.value.resetFields()
	}

	if (id) {
		getReturn_cars(id)
	}
}

const getReturn_cars = (id: number) => {
	useReturn_carsApi(id).then(res => {
		Object.assign(dataForm, res.data)
	})
}

const dataRules = ref({
})

// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate((valid: boolean) => {
		if (!valid) {
			return false
		}

		useReturn_carsSubmitApi(dataForm).then(() => {
			ElMessage.success({
				message: '操作成功',
				duration: 500,
				onClose: () => {
					visible.value = false
					emit('refreshDataList')
				}
			})
		})
	})
}

defineExpose({
	init
})
</script>
